import Vuex from "vuex"

function initialState() {
  return {
    user: JSON.parse(window.localStorage.getItem("user")) || null,
    startDate: null,
    endDate: null,
  }
}

const store = new Vuex.Store({
  state: initialState(),
  getters: {
    loggedIn(state) {
      return state.user != null
    },
  },
  mutations: {
    SET_USER(state, user) {
      window.localStorage.setItem("user", JSON.stringify(user))
      state.user = user
    },
    UPDATE_START_DATE(state, date) {
      state.startDate = date
    },
    UPDATE_END_DATE(state, date) {
      state.endDate = date
    },
    CLEAR_DATE(state) {
      state.startDate = null
      state.endDate = null
    },
  },
})

export default store
